package org.example.test.service.impl;

import org.example.test.dal.entity.Coupons;
import org.example.test.dal.mapper.CouponsMapper;
import org.example.test.model.CouponsModel;
import org.example.test.service.CouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class CouponsServiceImpl implements CouponsService {

    @Autowired
    CouponsMapper couponsMapper;

    @Override
    public Coupons selectCoupons(Long merchantId) {
        Coupons coupons = couponsMapper.selectByMerchantId(merchantId);
        return coupons;
    }

    @Override
    public void addCoupons(CouponsModel couponsModel) {
        Coupons coupons = new Coupons();
        coupons.setAmount(BigDecimal.valueOf(couponsModel.getAmount()));
        coupons.setMerchantId(couponsModel.getMerchantId());
        coupons.setStatus(couponsModel.getStatus());
        coupons.setClaimedCount(0);
        coupons.setTotalCount(couponsModel.getTotalCount());
        couponsMapper.insert(coupons);
    }

    @Override
    public void deleteCoupons(Long merchantId) {
        Coupons coupons = couponsMapper.selectByMerchantId(merchantId);
        couponsMapper.deleteByPrimaryKey(coupons.getId());
    }
}
